package com.quectel.sdk.simulation.parkinggate;

import com.fasterxml.jackson.core.type.TypeReference;
import com.quectel.sdk.ex.simulation.QueryDeviceException;
import com.quectel.sdk.simulation.SimulationBaseClient;
import com.quectel.sdk.simulation.basemodel.SimulationDevice;
import com.quectel.sdk.simulation.parkinggate.model.AuthCarReq;
import com.quectel.sdk.simulation.parkinggate.model.CarInfoResp;
import com.quectel.util.common.Response;
import com.quectel.util.kit.PageFetchKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 在这里编写说明
 *
 * @author: rananxin
 * @email: ryan.ran@quectel.com
 * @date: 2022-10-19 11:01:00
 */
public class ParkingGateClient extends SimulationBaseClient {

    private static final Logger LOGGER = LoggerFactory.getLogger(ParkingGateClient.class);

    public boolean open(String code) {
        String apiPath = "/guard/open";
        Response<Object> resp = requester.post(apiPath, code, new TypeReference<Response<Object>>() {
        });
        if (resp.isSuccess()) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    public void auth(AuthCarReq authCarReq) {
        String apiPath = "/car/auth";
        requester.post(apiPath, authCarReq, new TypeReference<Response<Object>>() {
        });
    }

    public List<CarInfoResp> cars() {
        String apiPath = "/car/list";
        Map<String, Object> params = new HashMap<>();
        return PageFetchKit.fetchAll(p -> requester.get(apiPath, p, new TypeReference<Response<List<CarInfoResp>>>() {
        }).getData(), params);
    }

    public Response<Object> sub(String code, String callBackUrl) {
        String apiPath = "/event/sub";
        Map<String, Object> params = new HashMap<>();
        params.put("code", code);
        params.put("callBackUrl", callBackUrl);
        return requester.post(apiPath, params, new TypeReference<Response<Object>>() {
        });
    }

    /**
     * 获取道闸设备
     *
     * @return
     */
    public List<SimulationDevice> getParkingGates() {
        String apiPath = "/device/queryByDeviceTypeCode";
        Map<String, Object> params = new HashMap<>();
        params.put("deviceTypeCode", "SimulationParkingGate");
        Response<List<SimulationDevice>> response = requester.post(apiPath, params, new TypeReference<Response<List<SimulationDevice>>>() {
        });
        if (response.isSuccess()) {
            return response.getData();
        }
        throw new QueryDeviceException("Obtain device failed");
    }

}
